/*
*  Copyright 2019-2023 CY
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.xiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.modules.xiao.domain.CgDesignInfo;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.xiao.service.CgDesignInfoService;
import me.zhengjie.modules.xiao.domain.vo.CgDesignInfoQueryCriteria;
import me.zhengjie.modules.xiao.mapper.CgDesignInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author cg
* @date 2024-06-20
**/
@Service
@RequiredArgsConstructor
public class CgDesignInfoServiceImpl extends ServiceImpl<CgDesignInfoMapper, CgDesignInfo> implements CgDesignInfoService {

    private final CgDesignInfoMapper cgDesignInfoMapper;

    @Override
    public PageResult<CgDesignInfo> queryAll(CgDesignInfoQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(cgDesignInfoMapper.findAll(criteria, page));
    }

    @Override
    public List<CgDesignInfo> queryAll(CgDesignInfoQueryCriteria criteria){
        return cgDesignInfoMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(CgDesignInfo resources) {
        resources.setId(null);
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CgDesignInfo resources) {
        CgDesignInfo cgDesignInfo = getById(resources.getId());
        cgDesignInfo.copy(resources);
        saveOrUpdate(cgDesignInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<CgDesignInfo> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CgDesignInfo cgDesignInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("主表id", cgDesignInfo.getDesignId());
            map.put("收款时间", cgDesignInfo.getCollectionTime());
            map.put("收款金额", cgDesignInfo.getAmountCollected());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void deleteByDesignId(Integer id) {
        QueryWrapper<CgDesignInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("design_id", id);
        remove(queryWrapper);
    }
}